if (typeof QSAPI == "undefined") {
	var QSAPI = {
		Widget : {}
	};
} else if (!QSAPI.Widget) {
	QSAPI.Widget = {};
}
QSAPI.Widget.ChartInterface={
	onDataReady:function(data){
		
	},
	setIdentification:function(){
		
	},
	getIdentification:function(){
		
	}
};

QSAPI.Widget.MiniChartDataSource=function(id){
	this.id= id||new Date().getTime();
	this.cfg = null;
	this.infoList =[];
	this.freq = null;
	this.needUpdateIntradyData =true;
	this.method = "getIntradayData";
};
QSAPI.Widget.MiniChartDataSource.prototype={
	_genInfo:function(tickerObject,cfg){
		var infoTemp=null,info=[];
		for(var i=0,l=tickerObject.length;i<l;i++){
			infoTemp={
				symbol: tickerObject[i]["getField"]("performanceId"),
	            days: cfg["days"]?cfg["days"]:1,
	            datapoints: {
	                p: (cfg.showReturn?"return":"price")
	            }
			};
			if(cfg.showPreClose){
				infoTemp.datapoints["indicators"]=[{name:"PrevClose",p:[]}];
			}
			info.push(infoTemp);
		}
		return info;
	},
	subscribe:function(tickerObject,cfg){
		var infoList=[],pid=null,infoTemp={};
		var tickerList = typeof(tickerObject)=="object"&&tickerObject.length>=0?tickerObject:[tickerObject];
		QSAPI.DataManager.Chart.registerSymbol(this,tickerList);//chardatamanager need gkey
		infoList=this._genInfo(tickerList,cfg);
		this.cfg = cfg;
		if(this.needUpdateIntradyData){
			QSAPI.DataManager.Chart.option.needUpdateIntradyData = true;
		}
        var freq=cfg["frep"]?cfg["frep"]:1;
        this.today = RTQ_ChartUtil.toDateIndex(new Date());
        var method = "getIntradayData";
        if (this.today - cfg["startIndex"] > 90) {//>90 
            method ="getHistoricalData";
            freq="d";
        }
		this.infoList=infoList;
		this.freq = freq;
		this.cfg = cfg;
		this.method = method;
		this._freshData();
	},
	refreshIntradayChart:function(){//type, pid
		this._freshData();
	},
	_format:function(s){
		if(typeof(QS_DateFmt)!="undefined"&&QS_DateFmt&&typeof(QS_DateFmt.getTimeString)=="function"){
			return QS_DateFmt.getTimeString(s);
		}
		return s;
	},
	_freshData:function(){
		var self = this;
        QSAPI.DataManager.Chart[this.method](this.infoList, this.freq, this.cfg["startIndex"]?this.cfg["startIndex"]:this.today, this.today, function(data){
            var xAxis =[],list=null,listLength=0,itemLength=0;
            for(var m=0,n=data.length;m<n;m++){
            	xAxis=[];
            	list =data[m].price.data;
            	listLength=list.length;
            	itemLength=listLength>0?list[0].length:0;
	            if(self.cfg.days==1){
	                for(var i =0;i<listLength;i++){
	                    var a = new Date(list[i][itemLength-1]);
	                    //record each 30'm xAxis
                        var t = self._format(a);
						if(self._isHalf(t)){
	                        xAxis.push([i,t]);
	                    }
	                }
	            }else if(self.cfg.days==5){
	                var k=0;
	                for(var i =0;i<listLength;i++){
	                    var d = new Date(list[i][itemLength-1]);
	                    if(k!=d.getDate()){
	                        var t = QS_DateFmt.Mo[d.getMonth()]+" "+d.getDate();
	                        xAxis.push([i,t]);
	                    }
	                    k=d.getDate();
	                }
	            }else{
	                xAxis=getTicks(data[m].price.data,self.freq);//chartDataManage
	            }
	            data[m].price.xAxis=xAxis;
	            break;
	        }
            self.onDataReady(data);
        });
	},
	_isHalf:function(t){
		var reg=/(:00)|(:30)/ig;
		return reg.test(t);
	},
	unSubscribe:function(tickerObject){
		QSAPI.DataManager.Chart.unregisterSymbol(this,tickerObject);
	},
	onDataReady:function(){
		
	},
	setIdentification:function(id){
		this.id = id;
	},
	getIdentification:function(){
		return this.id;	
	}
};

QSAPI.Widget.MiniChart = function(container,cfg,callback,sdkid){
	this.container = container;
	this.id=sdkid||new Date().getTime();
	this.cfg=$.extend(true,{ticker:"",mutilColor:{},width:400,height:200,needUpdateIntradyData:true},cfg);
	this.dataObj = null;
	this.drawObj = null;
	this.cb = {
		drawStart:function(){},
		drawEnd:function(){}
	};
	$.extend(true,this.cb,callback);
	this._init();
};

QSAPI.Widget.MiniChart.prototype={
	changeOutSize:function(w,h){
		this.drawObj.changeOutSize(w,h);
	},
	setLineWidth:function(width){
		this.drawObj.setLineWidth(width);
	},
	setPercision:function(decimal){
		this.drawObj.setPercision(decimal);
	},
	subscribe: function(tickerObject, cfg){
		this.dataObj.subscribe(tickerObject, cfg);
	},
	getXorYPosition:function(index,bool){
		return this.drawObj.axis[bool?"y":"x"].getCanvasPositionByPrice(index);
	},
	unSubscribe:function(tickerObject){
		this.dataObj.unSubscribe(tickerObject);
	},
	_init:function(){
		var self = this;
		var _cfg = {canvasWidth:this.cfg.width,canvasHeight:this.cfg.height,showX:this.cfg.showX,showY:this.cfg.showY,lineType:this.cfg.lineType};
		if(typeof(this.cfg.color)!="undefined"){
			_cfg["color"]=this.cfg.color;
		}
		if(typeof(this.cfg.percision)!="undefined"){
			_cfg["percision"]=this.cfg.percision;
		}
		if(typeof(this.cfg.lineWidth)!="undefined"){
			_cfg["lineWidth"]=this.cfg.lineWidth;
		}
		if(typeof(this.cfg.showYScaleNumber)!="undefined"){
			_cfg["showYScaleNumber"]=this.cfg.showYScaleNumber;
		}
		if(typeof(this.cfg.showYScale)!="undefined"){
			_cfg["showYScale"]=this.cfg.showYScale;
		}
		if(typeof(this.cfg.percision)!="undefined"){
			_cfg["percision"]=this.cfg.percision;
		}
		if(typeof(this.cfg.xLableMinWidth)!="undefined"){
			_cfg["xLableMinWidth"]=this.cfg.xLableMinWidth;
		}
		if(typeof(this.cfg.showYPrecent)!="undefined"){
			_cfg["showYPrecent"]=this.cfg.showYPrecent;
		}
		if(typeof(this.cfg.showYPlus)!="undefined"){
			_cfg["showYPlus"]=this.cfg.showYPlus;
		}
		if(typeof(this.cfg.alternateXLable)!="undefined"){
			_cfg["alternateXLable"]=this.cfg.alternateXLable;
		}
		if(typeof(this.cfg.showX)!="undefined"){
			_cfg["showX"]=this.cfg.showX;
		}
		if(typeof(this.cfg.showY)!="undefined"){
			_cfg["showY"]=this.cfg.showY;
		}
		if(typeof(this.cfg.xLableCount)!="undefined"){
			_cfg["xLableCount"]=this.cfg.xLableCount;
		}
		if(typeof(this.cfg.customLabel)!="undefined"){
			_cfg["customLabel"]=this.cfg.customLabel;
		}
		if(typeof(this.cfg.marginTop)!="undefined"){
			_cfg["marginTop"]=this.cfg.marginTop;
		}
		if(typeof(this.cfg.customYLabelContainer)!="undefined"){
			_cfg["customYLabelContainer"]=this.cfg.customYLabelContainer;
		}
		if(typeof(this.cfg.align)!="undefined"){
			_cfg["align"]=this.cfg.align;
		}
		this.drawObj = new chartCanvas(this.container,_cfg);
		
		this._initDataSource();
		this.dataObj.onDataReady=function(data){
			self._drawChart(data);
		};
	},
	_drawChart:function(data){
		if (this.cb.drawStart && typeof(this.cb.drawStart) == 'function') {
        	this.cb.drawStart(data);//need xAxis and other info
        }
		if(data.length==1){
			this.drawObj.drawChart(data[0].price);
		}else{
			var dataList=[],ticker=null;
			for(var i=0,l=data.length;i<l;i++){
				ticker = data[i].symbol;
				dataList.push({data:data[i].price.data,styleCfg:this.cfg.mutilColor[ticker]});
			}
			this.drawObj.drawMutiChart(dataList);
		}
        if (this.cb.drawEnd && typeof(this.cb.drawEnd) == 'function') {
        	this.cb.drawEnd(data);//need xAxis and other info
        }
        this.data=data;//for change out size
	},
	setMutilColor:function(mutilColor){//for set draw mutil-line
		this.cfg.mutilColor=mutilColor;
	},
	_initDataSource:function(){
		if(typeof(this.dataObj)=="undefined"||this.dataObj==null){
			if(typeof(QSAPI.Widget.MiniChartDataSource)=="function"){
				this.dataObj = new QSAPI.Widget.MiniChartDataSource(this.id);
				this.dataObj.needUpdateIntradyData = this.cfg.needUpdateIntradyData;
			}else{
				this.dataObj =QSAPI.Widget.ChartInterface;
			}
		}
	}
};











